<?php  
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Users extends Controller
{
	public function users()
	{

     $list = Db::name('tb_users')->select();
     $this->assign('list',$list);  
    




     return $this->fetch('users/users');
	}


	public function change($id)
	{  
	   $list = Db::name('tb_users')->where('id',$id)->select();
	

	   if ($list[0]['status'] ==1) {
	      $data=2;
	   }else{
	   	   $data = 1;
	   }
      
    
	   $result = Db::name('tb_users')->where('id',$id)->update(['status'=>$data]);

	 
		if ($result) {
		     
    		 return $result;
		}	     
	  
	  }

	  //用户增加
   public function add(Request $request)
   {   

       
  
    // 获取表单上传文件
        $file = $request->file('image');
        if (empty($file)) {
            $this->error('请选择上传文件');
        }
    // 移动到框架应用根目录/public/uploads/ 目录下
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
       $icon=$info->getSaveName();
       $icon = str_replace('\\','/',$icon);

    
   	  //接收post数据
        $list = Request::instance()->post();
        // var_dump($list);die;
        $time=date('Y-m-d H:i:s',time());
      //处理数据
        $data=['phone'=>$list['phone'],
               'password'=>$list['password'],
               'name'=>$list['name'],
               'sex'=>$list['sex'],
               'introduce'=>$list['dex'],
               'birthday'=>$list['birthday'],
               'address'=>$list['address'],
               'icon'=>$icon,
               'createtime'=>$time
              ];

        $result= Db::name('tb_users')->data($data)->insert();

          if ($result >0) {
          return $this->success('添加成功',url('admin/Users/users'));
       }else{
          return $this->success('添加失败');
       }



   }



    //用户编辑第一步
   public function edit($id)

   {


    $list = Db::name('tb_users')->where('id',$id)->select();
    // var_dump($list);die;
    $data=$list[0];
    $this->assign('data',$data); 

    
    return $this->fetch('users/update');
   	 
   }


    //用户编辑第二步
   public function edits(Request $request)
   {
   	 // 获取表单上传文件
        $file = $request->file('image');
        if (empty($file)) {
            $this->error('请选择上传文件');
        }
    // 移动到框架应用根目录/public/uploads/ 目录下
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
       $icon=$info->getSaveName();
       $icon = str_replace('\\','/',$icon);


         //接收post数据
        $list = Request::instance()->post();

         //处理数据
        $data=['phone'=>$list['phone'],
               'password'=>$list['password'],
               'name'=>$list['name'],
               'sex'=>$list['sex'],
               'introduce'=>$list['dex'],
               'birthday'=>$list['birthday'],
               'address'=>$list['address'],
               'icon'=>$icon,
               
              ];


        $result= Db::name('tb_users')->where("id",$list['id'])->update($data);


          if ($result >0) {
          return $this->success('编辑成功',url('admin/Users/users'));
       }else{
          return $this->success('编辑失败');
       }



    
   }



    //用户删除
   public function delete($id)
   {

   	 $result = Db::name('tb_users')->delete($id);
     
     if ($result >0 ) {
       $info['status'] = true;
     }else{
       $info['status'] = false;
     }

     return $info;




   	 return $this->fetch('users/users');
   }





   
}